/* This file is part of the Crimsoon project
  * Copyright (C) 2009 Eduardo Henrique <eduardohleite@gmail.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
*/

#include <QWidget>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QFileInfo>
#include <QMetaType>

#include "scanner.h"
#include "gallery.h"
#include "musicfile.h"
#include "album.h"

void Scanner::scanDir(QString path)
{
    QDir dir(path);

    emit scanningDirChanged(path);

    if (!dir.exists())
    {
        return;
    }

    scanDirForAudioFiles(dir);

    if (scanSubDir)
    {
        foreach (QString directory, dir.entryList(QDir::AllDirs))
        {
            if (directory != "." && directory != "..")
            {
                scanDir(QFileInfo(dir, directory).absoluteFilePath());
            }
        }
    }
}

void Scanner::scanDirForAudioFiles(QDir dir)
{
    MusicFile   musicFile;
    Gallery     gallery;
    Album       *album;
    AlbumStatus albumStatus;
    QFileInfo   fileResolver;
    QStringList filters;
    QString     filename;
    QString     path;
    qint32      dirCode;
    bool        hasPicture;

    path = dir.absolutePath();

    gallery.beginBulk();

    if (gallery.hasPath(path))
    {
        gallery.removeFilesFromDir(path);
        dirCode = gallery.addScanDir(path);
    }

    filters << "*.mp3";
    foreach (QString file, dir.entryList(filters, QDir::Files))
    {
        fileResolver = QFileInfo(dir, file);
        filename = fileResolver.absoluteFilePath();
        musicFile = gallery.getMusicFileInfo(filename);

        if ((!musicFile.artist.isEmpty()) && (!musicFile.album.isEmpty()))
        {
            album = new Album();

            album->artist = musicFile.artist;
            album->title = musicFile.album;

            albumStatus = gallery.hasAlbum(*album);

            hasPicture = (musicFile.picture != NULL && (!musicFile.picture->isEmpty()) &&
                          musicFile.picture->at(0) != '\0');

            if ((albumStatus == AlbumNotExists || albumStatus == AlbumNoPicture) && hasPicture)
            {
                album->picture = musicFile.picture;
            }

            if (albumStatus == AlbumNotExists)
            {
                gallery.addAlbum(*album);
            }
        }

        gallery.addAudioFile(musicFile, dirCode);

        emit musicFileFound(musicFile);
    }

    gallery.endBulk();
}

void Scanner::run()
{
    qRegisterMetaType<MusicFile>("MusicFile");

    scanDir(path);
}

Scanner::Scanner(QWidget *parent, QString path, bool scanSubDirectories)
{
    this->parent = parent;
    this->path   = path;
    scanSubDir   = scanSubDirectories;
}

